package tripx.site;

import java.io.File;
import java.util.HashMap;

import tripx.server.Service;
import tripx.sql.SqlHandler;
import tripx.sql.UpdateSqlHandler;

/**
 * provide method for query the site information
 * 
 * @author zhb
 * 
 */
public class SiteService extends Service {

	private SiteServiceConfig config;
	/**
	 * handle the operation on the city table
	 */
	private UpdateSqlHandler cityHandler;
	/**
	 * handle the operation on the site table
	 */
	private UpdateSqlHandler siteHandler;
	/**
	 * handle the operation on the food table
	 */
	private UpdateSqlHandler foodHandler;

	public SiteService() {
		config = SiteServiceConfig.getInstance();

		cityHandler = new UpdateSqlHandler(config.getCityTableName());
		siteHandler = new UpdateSqlHandler(config.getSiteTableName());
		foodHandler = new UpdateSqlHandler(config.getFoodTableName());

		sqlHandler = new SqlHandler(config.getQueryActionList());
	}

	/**
	 * add a new city information
	 * 
	 * @param city
	 * @return
	 */
	public boolean addCity(DbCity city) {
		String sql = cityHandler.getSqlAdd(city);
		if (cityHandler.getStatus() == 1 && invoker.executeUpdateSql(sql) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * add a new site information.
	 * 
	 * @param site
	 * @return
	 */
	public boolean addSite(DbSite site) {
		String sql = siteHandler.getSqlAdd(site);
		if (siteHandler.getStatus() == 1 && invoker.executeUpdateSql(sql) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * add a new food information
	 * 
	 * @param food
	 * @return
	 */
	public boolean addFood(DbFood food) {
		String sql = foodHandler.getSqlAdd(food);
		if (foodHandler.getStatus() == 1 && invoker.executeUpdateSql(sql) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * modify the site information
	 * 
	 * @param key
	 * @param newValue
	 * @param conditionMap
	 * @return
	 */
	public boolean modifySite(String key, String newValue,
			HashMap<String, String> conditionMap) {
		String sql = siteHandler.getSqlModify(key, newValue, conditionMap);
		if (siteHandler.getStatus() == 1 && invoker.executeUpdateSql(sql) >= 1) {
			return true;
		}
		return false;
	}

	/**
	 * download the site image
	 * 
	 * @param path
	 * @param formatName
	 * @return
	 */
	public byte[] downloadSiteImage(String path, String formatName) {
		logger.info("Dowoload Image:");
		logger.info("\tPath: " + config.getSiteImageBaseDir() + "/" + path);
		return super.downloadImage(config.getSiteImageBaseDir() + "/" + path,
				formatName);
	}

	/**
	 * download the food image
	 * 
	 * @param siteName
	 * @param formatName
	 * @return
	 */
	public byte[] downloadFoodImage(String path, String formatName) {
		logger.info("Dowoload Image:");
		logger.info("\tPath: " + config.getFoodImageBaseDir() + "/" + path);
		return super.downloadImage(config.getFoodImageBaseDir() + "/" + path,
				formatName);
	}

	/**
	 * upload an Image on the specified site
	 * 
	 * @param imageData
	 * @param cityName
	 * @param siteName
	 * @return
	 */
	public boolean uploadSiteImage(byte[] imageData, String cityName,
			String siteName) {
		String fullPath = config.getSiteImageBaseDir() + "/" + cityName + "/"
				+ siteName + ".jpg";
		String folderPath = fullPath.substring(0, fullPath.lastIndexOf("/"));
		File folder = new File(folderPath);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		return super.uploadImage(imageData, fullPath, "JPEG");
	}

	/**
	 * upload an Image on the specified food
	 * 
	 * @param imageData
	 * @param cityName
	 * @param foodName
	 * @return
	 */
	public boolean uploadFoodImage(byte[] imageData, String cityName,
			String foodName) {
		String fullPath = config.getFoodImageBaseDir() + "/" + cityName + "/"
				+ foodName + ".jpg";
		String folderPath = fullPath.substring(0, fullPath.lastIndexOf("/"));
		File folder = new File(folderPath);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		return super.uploadImage(imageData, fullPath, "JPEG");
	}
}
